const { BasicCredentials, Region } = require('@huaweicloud/huaweicloud-sdk-core')
const { ShowAssetDetailRequest,VodClient } = require('@huaweicloud/huaweicloud-sdk-vod')
class HuaweiCloud {
  static AK = 'HSMRUTVGPVWWIWMUSCTN'
  static SK = 'eo9LKUO4CCBmiEaWsgQfSHZEDNPBWQqaZeBnJjgU'
  static PROJECT_ID = '4ed81f307cba4d89abf374dbb29d896e'
  static REGION = 'cn-north-4'
  static ENDPOINT = 'https://vod.cn-north-4.myhuaweicloud.com'

  static getVodClient() {
    // 使⽤永久AK、SK Region级服务
    const auth = new
    BasicCredentials().withAk(HuaweiCloud.AK).withSk(HuaweiCloud.SK).withProjectId(HuaweiCloud.PROJECT_ID)
    // 初始化指定云服务的客户端
    return VodClient.newBuilder().withCredential(auth).withRegion(new
    Region(HuaweiCloud.REGION, HuaweiCloud.ENDPOINT)).build()
     }
    // 获取视频播放地址
    static async getVideoUrl(assetId) {
    let vodClient = HuaweiCloud.getVodClient()
    let detailRequest = new ShowAssetDetailRequest()
    detailRequest.withAssetId(assetId)
    let detail = await vodClient.showAssetDetail(detailRequest)
    let outputs = detail.transcode_info?.output
    if (!outputs || outputs.length <= 0) return ['']
    let urlList = []
    for (let output of outputs) {
    let { bit_rate, duration, video_size } = output.meta_data
    bit_rate > 0 && duration && duration > 0 && video_size > 0 &&
    urlList.push(output.url)
     }
    return urlList
    }
  }
  module.exports = HuaweiCloud